﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace eBook.Core
{
  public enum ISBNValidationError
  {
    NotYetValidated,
    None,
    InvalidInputSize,
    InvalidCharacters,
    CheckDigitError
  }
  
  public abstract class ISBN
  {     
    protected string _isbnval;
    protected string _strippedISBN = string.Empty;

    #region Constructors
    
    protected ISBN( string isbnValue )
    {
      _strippedISBN = StripDelimiters( isbnValue );
      _isbnval      = isbnValue;
    } 
    
    #endregion

    #region Properties
    
    public ISBNValidationError ValidationError {get;set;}

    public string InputString
    {
      get { return _isbnval; }
    }
    
    public string StrippedISBN
    {
      get { return _strippedISBN;}
    }
    
    #endregion

    #region Abstract methods
    
    public abstract string Transform();

    public abstract bool Validate();

    public abstract string Hyphenate();

    #endregion

    #region Overrides
    
    public override string ToString()
    {
      return string.IsNullOrEmpty( _isbnval ) ? string.Empty : this.Hyphenate();
    } 
    
    #endregion

    #region Utility methods
    
    protected short CharToDigitValue( char c )
    {
      return Convert.ToInt16( c.ToString() );
    }

    private string StripDelimiters( string isbnval )
    {
      string s = string.Empty;
      if ( !string.IsNullOrEmpty(isbnval) )
        s = new Regex( @"[- —–]" ).Replace( isbnval, string.Empty ).ToUpper();

      return s;
    } 
    
    #endregion
    
  }
}
